package megrez.keycloak.common;

import org.springframework.util.StringUtils;

import lombok.experimental.UtilityClass;

/**
 * 字符串格式化
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@UtilityClass
public class StringHelper {
    /**
     * 驼峰转换成下划线
     * Convert aStringUnderscored into A_STRING_UNDERSCORED.
     * 
     * @param camelCaseString the string to convert
     * 
     * @return the underscored string
     */
    public String camelCaseToUnderscore(final String camelCaseString) {
        if (!StringUtils.hasText(camelCaseString)) {
            return camelCaseString;
        }
        StringBuilder sb = new StringBuilder();
        for (String camelPart : camelCaseString.split("(?<!(^|[A-Z]))(?=[A-Z])|(?<!^)(?=[A-Z][a-z])")) {
            if (sb.length() > 0) {
                sb.append("_");
            }
            sb.append(camelPart.toUpperCase());
        }
        return sb.toString();
    }
}
